﻿//-------------------------------------------------------------------------------------------------
// <copyright file="YahooStockDataUrl.cs" company="Andrew Craig Keller">
//  Copyright (c) Andrew Craig Keller.  All rights reserved.
// </copyright>
//-------------------------------------------------------------------------------------------------

namespace StockQuoteServices
{
    using System.Web;

    /// <summary>
    /// Constructs a URL that can be used to retrieve basic data for a stock from Yahoo's CSV API.
    /// </summary>
    internal class YahooStockDataUrl
    {
        /// <summary>
        /// The base url to be used for all requests.
        /// </summary>
        private const string BaseUrl = "http://download.finance.yahoo.com/d/quotes.csv?s=";

        /// <summary>
        /// A Yahoo-specific format string specifying the data to be included in the CSV file.
        /// </summary>
        /// <remarks>
        /// <see href="http://www.gummy-stuff.org/Yahoo-data.htm"/>
        /// </remarks>
        private const string FormatOptions = "nl1";

        /// <summary>
        /// The ticker symbol of the stock for which data is to be retrieved.
        /// </summary>
        private string tickerSymbol;

        /// <summary>
        /// Initializes a new instance of the <see cref="YahooStockDataUrl"/> class for the stock
        /// with the given ticker symbol.
        /// </summary>
        /// <param name="tickerSymbol">
        /// the ticker symbol of the stock for which data is to be retrieved
        /// </param>
        public YahooStockDataUrl(string tickerSymbol)
        {
            this.tickerSymbol = HttpUtility.UrlEncode(tickerSymbol);
        }

        /// <summary>
        /// Prevents a default instance of the <see cref="YahooStockDataUrl"/> class from being created.
        /// </summary>
        private YahooStockDataUrl()
        {
        }

        /// <summary>
        /// Gets the string representing of the URL to the resource providing data for the stock.
        /// </summary>
        /// <returns>
        /// the string representation of the URL to the resource providing data for the stock
        /// </returns>
        public override string ToString()
        {
            return BaseUrl
                + HttpUtility.UrlEncode(this.tickerSymbol)
                + "&f="
                + FormatOptions
                + "&e=.csv";
        }
    }
}
